<?php

namespace App\Admin\Controllers;

use App\Store;
use App\StoresInfo;
use App\Subsidy;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

class SubsidyController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '任务';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Subsidy());
        $arr = ['待审核', '进行中' , '已中止'];
        $grid->filter(function($filter) use($arr) {

            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->equal('id', '编号');
            $filter->equal('merchant.id', '商户ID');
            // 设置datetime类型
            $filter->between('created_at', '发布时间')->datetime();
            // 在这里添加字段过滤器
            $filter->like('stores.title', '店铺名称');

            $filter->in('storesInfo.type', '外卖平台')->multipleSelect([1=>'美团', 2=>'饿了么']);
            $filter->in('status', '任务状态')->multipleSelect($arr);

        });

        $grid->model()->where('is_deleted', 0)->collection(function( Collection $collection){
            $data = DB::table('users_orders')->whereIn('status', [0, 20, 25, 35, 40, 50])
                ->groupBy('sub_id')->pluck(DB::raw('count(*) as total'), 'sub_id');
            foreach ($collection as $item) {
                if( $item->status == 1 ){
                    $item->sellNum = ('累计销量：'.(isset($data[$item->id]) ? $data[$item->id] : 0)).',今日:'.($item->single_day_count-$item->current_rest).'/'.$item->single_day_count;
                }else{
                    $item->sellNum = ('累计/日投放'.(isset($data[$item->id]) ? $data[$item->id] : 0)).'/'.$item->single_day_count;
                }

            }

            return $collection;
        });

        $grid->column('id', '任务编号')->sortable();
        $grid->column('storesInfo.title', '店铺名');

        $grid->column('max_return', '满返')->display(function() {
            return "满".$this->max_money."返".$this->return_money;
        });
        $grid->column('time', '任务时间')->display(function() {
            return $this->start_time."~".$this->overdue."（".$this->day_count."天）";
        });
        $grid->column('storesInfo.type', '外卖平台')->display(function($value) {
            return $value == 1 ? '美团' : '饿了么';
        });
        $grid->column('sellNum', '销量');
        $grid->column('uid', '商户ID');


        $grid->column('status', "任务状态")->display(function($status) use($arr){

            return $arr[$status];
        });

        $grid->column('is_frozen', "冻结状态")->using(['正常','已冻结']);
        // 设置text、color、和存储值
        $states = [
            'on'  => ['value' => 1, 'text' => '取消推荐', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '推荐', 'color' => 'default'],
        ];
        $grid->column('is_recommend', '是否推荐')->switch($states);
        // 全部关闭
        $grid->disableActions();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Subsidy::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('uid', __('Uid'));
        $show->field('type', __('Type'));
        $show->field('is_recommend', __('Is recommend'));
        $show->field('max_money', __('Max money'));
        $show->field('return_money', __('Return money'));
        $show->field('return_rate', __('Return rate'));
        $show->field('good_comment_type', __('Good comment type'));
        $show->field('day_count', __('Day count'));
        $show->field('single_day_count', __('Single day count'));
        $show->field('current_rest', __('Current rest'));
        $show->field('status', __('Status'));
        $show->field('overdue', __('Overdue'));
        $show->field('is_deleted', __('Is deleted'));
        $show->field('store_id', __('Store id'));
        $show->field('store_info_id', __('Store info id'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Subsidy());

        $form->number('uid', __('Uid'));
        $form->switch('type', __('Type'));
        $form->switch('is_recommend', __('Is recommend'));
        $form->decimal('max_money', __('Max money'))->default(0.00);
        $form->decimal('return_money', __('Return money'))->default(0.00);
        $form->decimal('return_rate', __('Return rate'))->default(0.00);
        $form->switch('good_comment_type', __('Good comment type'))->default(1);
        $form->number('day_count', __('Day count'));
        $form->number('single_day_count', __('Single day count'));
        $form->number('current_rest', __('Current rest'));
        $form->switch('status', __('Status'));
        $form->date('overdue', __('Overdue'))->default(date('Y-m-d'));
        $form->switch('is_deleted', __('Is deleted'));
        $form->number('store_id', __('Store id'));
        $form->number('store_info_id', __('Store info id'));

        return $form;
    }
}
